package com.system.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.bean.Admin;
import com.system.common.bean.Goods;
import com.system.common.service.IAdminService;
import com.system.common.service.IGoodsService;
import com.system.common.util.PasswordUtil;
import com.system.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @DubboReference
    private IGoodsService goodsService;
    @PostMapping("/add")
    public Result add(@RequestBody Goods goods){
        goodsService.addGoods(goods);
        return Result.ok("添加商品成功");
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        goodsService.deleteGoods(id);
        return Result.ok("删除商品成功");
    }
    @PutMapping("/update")
    public Result update(@RequestBody Goods goods){
        goodsService.updateGoods(goods);
        return Result.ok("修改商品成功");
    }
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody Goods goods){
        goodsService.updateStatus(goods.getId(), goods.getStatus());
        return Result.ok("修改商品状态成功");
    }

    @GetMapping("/list")
    public Result list(){
        return Result.ok("获取管理员列表成功",goodsService.getGoodsList());
    }
    @GetMapping("/page")
    public Result page(Goods goods,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        return Result.ok(goodsService.getPage(goods, new Page<>(pageNum, pageSize)));
    }
    @GetMapping("/getGoodsById/{id}")
    public Result getGoodsById(@PathVariable Integer id){
        return Result.ok("获取商品成功",goodsService.getGoodsById(id));
    }
    @GetMapping("/getGoodsByName/{name}")
    public Result getGoodsByName(@PathVariable String name){
        return Result.ok("获取商品成功",goodsService.getGoodsByName(name));

  }

    @PutMapping("/reduceStock")
    public Result reduceStock(
            @RequestParam Integer id,
            @RequestParam Integer count
    ) {
        goodsService.reduceStock(id, count);
        return Result.ok("扣减库存成功");
    }

}


